package filter;

import Utils.db.DruidUtils;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;

/**
 * @author d11yu
 */
@WebFilter(filterName = "TransactionFilter", urlPatterns = "/*")
public class TransactionFilter implements Filter {
    @Override
    public void init(FilterConfig config) throws ServletException {
    }

    @Override
    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest request0, ServletResponse response0, FilterChain chain) {
        try {
            chain.doFilter(request0, response0);
            DruidUtils.getInstance("jdbc.properties").commitAndClose();
        } catch (Exception e) {
            DruidUtils.getInstance("jdbc.properties").rollbackAndClose();
            e.printStackTrace();
            // 把异常抛给Tomcat服务器，统一展示错误页面
            throw new RuntimeException(e);
        }
    }
}
